package com.dubbo.product.controller;

import com.dubbo.commons.FtpUtil;
import com.dubbo.pojo.Product;
import com.dubbo.pojo.Users;
import com.dubbo.product.service.ProductService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.MulticastChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class ProductController {

    String path;
    @RequestMapping("/uploadFile")
    @ResponseBody
    public Map<String,Object> uploadFile(MultipartFile mf){
        String filename = mf.getOriginalFilename();
        String newFilename=System.currentTimeMillis()+filename;
        InputStream in = null;
        try {
            in = mf.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String,Object> map = new HashMap<>();
        //测试上传
        boolean flag = FtpUtil.uploadFile("192.168.92.129", 21, "ftpuser", "root", "/home/ftpuser/images","/20220421", newFilename, in);
        path ="http://192.168.92.129:9999/20220421/"+newFilename;
        if (flag){
            map.put("error",0);
            map.put("url",path);
        }else {
                map.put("error",1);
                map.put("message","错误信息");

        }

        return map;
    }

    @Autowired
    private ProductService productService;
    @RequestMapping(value = "/addProduct",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String addProduct(Product product){
        product.setImagepath(path);
        int i = productService.addProduct(product);
        if (i == 1){
            return "上架成功";
        }else {
            return "上架失败";
        }

    }

    @RequestMapping("/show")
    private String showList(Model model, HttpSession session){
        List<Product> products = productService.showList();
        model.addAttribute("products",products);
        Users user =(Users) session.getAttribute("user");
        if (user != null) {
            String name = user.getUname();
            model.addAttribute("username", name);
        }
        return "list" ;
    }

    @RequestMapping("/showOne/{id}")
    public String showOne(@PathVariable int id,Model model, HttpSession session){
        Product product = productService.showOne(id);

        List<Product> list = new ArrayList<>();
        list.add(product);
        model.addAttribute("products",list);
        Users user =(Users) session.getAttribute("user");
        if (user != null) {
            String name = user.getUname();
            model.addAttribute("username", name);
        }
        return "list" ;

    }


    @RequestMapping("/showOne2/{id}")
    public String showOne2(@PathVariable int id,Model model, HttpSession session){
        Product product = productService.showOne2(id);

        List<Product> list = new ArrayList<>();
        list.add(product);
        model.addAttribute("products",list);
        Users user =(Users) session.getAttribute("user");
        if (user != null) {
            String name = user.getUname();
            model.addAttribute("username", name);
        }
        return "list" ;

    }

    /**
     *  更新数据
     */
    @RequestMapping(value = "/update/{id}",produces = "text/html;chatset=utf-8")
    @ResponseBody
    public String updateCache(@PathVariable int id){
         productService.updateCache(id);
         return "修改成功";

    }


    /**
     * 副本修改，使用注解的
     * @param id
     * @return
     */
    @RequestMapping(value = "/update2/{id}",produces = "text/html;chatset=utf-8")
    @ResponseBody
    public String updateCache2(@PathVariable int id){
         productService.updateCache2(id);
         return "修改成功";

    }

    /**
     * 手动删除缓存
     */

    @RequestMapping(value = "/delete/{id}",produces = "text/html;chatset=utf-8")
    @ResponseBody
    public String deleteCache(@PathVariable int id){
        productService.deletecache(id);
        return "缓存删除成功";
    }
    /**
     * 注释删除缓存
     */
    @RequestMapping(value = "/delete2/{id}",produces = "text/html;chatset=utf-8")
    @ResponseBody
    public String deleteCache2(@PathVariable int id){
        productService.deletecache2(id);
        return "缓存删除成功";
    }


    /**
     * 设置一个防止用户重复访问的问题
     */
    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("/demo/{userid}")
    @ResponseBody
    public String Demo(@PathVariable String userid){
        Long aLong = productService.singTo(userid);
        if (aLong == 1){
            //一天后才能访问
            redisTemplate.boundValueOps(userid).expire(1, TimeUnit.DAYS);
            Long expire = redisTemplate.boundValueOps(userid).getExpire();
            System.out.println(expire);
            return "允许访问";
        }else {
            return "访问次数过多";
        }

    }


}
